package com.nari.kmd_dataservice.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ObjConvertListUtil {
    /**
     * 私有化构造器
     */
    private ObjConvertListUtil() {
    }


    /**
     * 集合分割
     * @param List 目标集合
     * @param num 每份多少条
     * @return
     */
    public static <M> List<List<M>> splitArrayList(List<M> List,int num){
        List<List<M>> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(List)) {
            //一次最多插入,更新num条
            int start = 0;
            int size = num;
            for (start = 0; start < List.size(); start = start + size) {
                if (start + num <= List.size()) {
                    size = num;
                }
                else{
                    size = List.size()-start;
                }
                resultList.add(List.subList(start, start + size));
            }
        }
        return resultList;
    }
}
